package com.lxp.userms.service.impl;

import com.lxp.userms.entity.Like;
import com.lxp.userms.mapper.LikeMapper;
import com.lxp.userms.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Override
    @Transactional
    public int addLike(Like like) {
        return likeMapper.insertLike(like);
    }

    @Override
    @Transactional
    public int removeLike(int postId, int userId) {
        return likeMapper.deleteLike(postId, userId);
    }

    @Override
    public int getLikeCount(int postId) {
        return likeMapper.countLikesByPostId(postId);
    }

    @Override
    public boolean isLiked(int postId, int userId) {
        Like like = likeMapper.checkLike(postId, userId);
        return like != null;
    }

    // 新增方法：删除指定 Post 的所有点赞记录
    @Override
    @Transactional
    public void deleteLikesByPostId(int postId) {
        likeMapper.deleteLikesByPostId(postId);
    }
}